package tv.youxing.android.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import tv.youxing.android.R;
import tv.youxing.android.adapter.FabulousAdapter;
import tv.youxing.android.custom.CustomToast;
import tv.youxing.android.entity.FabulousModel;
import tv.youxing.android.entity.VideoPlayerPostModel;
import tv.youxing.android.utils.AppConst;
import tv.youxing.android.utils.GsonUtils;
import tv.youxing.android.utils.ShareUtils;
import tv.youxing.android.utils.StaticClass;

public class FabulousActivity extends BaseActivity {

    @BindView(R.id.pull_refresh_grid)
    PullToRefreshGridView mPullRefreshListView;
    int curPageCount = 0;//当前页数

    FabulousModel allData;
    List<FabulousModel.ResultBean.DataBean> listDatas = new ArrayList<>();
    private FabulousAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fabulous);
        ButterKnife.bind(this);

        initData();
        showProgress("加载中");
        loadData();
    }


    void initData() {

        mAdapter = new FabulousAdapter(FabulousActivity.this, listDatas);
        mPullRefreshListView.setAdapter(mAdapter);


        mPullRefreshListView
                .setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<GridView>()
                {
                    @Override
                    public void onRefresh(
                            PullToRefreshBase<GridView> refreshView)
                    {
                        String label = DateUtils.formatDateTime(
                                getApplicationContext(),
                                System.currentTimeMillis(),
                                DateUtils.FORMAT_SHOW_TIME
                                        | DateUtils.FORMAT_SHOW_DATE
                                        | DateUtils.FORMAT_ABBREV_ALL);
                        // 显示最后更新的时间
//                        refreshView.getLoadingLayoutProxy()
//                                .setLastUpdatedLabel(label);

                        // 模拟加载任务
                        loadData();
                    }
                });

        mPullRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

               FabulousModel.ResultBean.DataBean m = listDatas.get(i);
                VideoPlayerPostModel model = new VideoPlayerPostModel();
                model.setMultimedia_id(m.getMultimedia_id());
                model.setTitle(m.getTitle());
                model.setIntro(m.getIntro());
                model.setCover(m.getCover());
                model.setDuration(m.getDuration());
                model.setPublish_at(m.getPublish_at());
                model.setPlay_url(m.getPlay_url());
                model.setType(m.getType());
                model.setPlay_count(m.getPlay_count());
                model.setComment_count(m.getComment_count());
                model.setLike_count(m.getLike_count());
                model.setCollection_count(m.getCollection_count());
                model.setStatus(m.getStatus());
                model.setCreated_at(m.getCreated_at());
                model.setUpdated_at(m.getUpdated_at());
                model.setAlbum_name(m.getAlbum_name());
                model.setIs_like(m.getIs_like());
                model.setIs_collectied(m.getIs_collectied());
                model.setVideo_share_url(m.getVideo_share_url());
                VideoPlayerPostModel.AlbumBean albumBean = new VideoPlayerPostModel.AlbumBean();
                albumBean.setAlbum_id(m.getAlbum().getAlbum_id());
                albumBean.setName(m.getAlbum().getName());
                albumBean.setCover(m.getAlbum().getCover());
                albumBean.setIs_order(m.getAlbum().getIs_order());
                model.setAlbum(albumBean);

                Intent intent = new Intent(FabulousActivity.this, SubscribeDeteilsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("data", model);
                intent.putExtras(bundle);
                FabulousActivity.this.startActivity(intent);
            }
        });


    }

    private void loadData() {
        //post请求简洁版实现

        curPageCount++;
        HttpParams params = new HttpParams();
        params.putHeaders("Authorization", "Bearer " + ShareUtils.getString(this, AppConst.TOKEN, ""));
        params.put("page",curPageCount);
        RxVolley.post(StaticClass.myFabulous, params, new HttpCallback() {
            @Override
            public void onSuccess(String t) {

                hideProgress();

                allData = GsonUtils.GsonToBean(t, FabulousModel.class);

                if (allData.getCode() == AppConst.TOKENERROR) {
                    FabulousActivity.this.startActivity(new Intent(FabulousActivity.this,LoginActivity.class));
                    return;
                }

                if (allData.getCode() == 200) {
                    if (allData.getResult().getData().size() > 0) {
                        listDatas.addAll(allData.getResult().getData());
                        mAdapter.notifyDataSetChanged();
                    } else {
                        CustomToast.INSTANCE.showToast("没数据了");
                    }


                    if (mPullRefreshListView.isRefreshing()) {
                        mPullRefreshListView.onRefreshComplete();
                    }



                } else {
                    CustomToast.INSTANCE.showToast("无数据");
                }
            }
        });

    }
}
